package cc.comac.mom.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipReqularCheckItemService;
import cc.comac.mom.bean.EquipReqularCheck;
import cc.comac.mom.bean.EquipReqularCheckBean;
import cc.comac.mom.bean.EquipReqularCheckItem;
import cc.comac.mom.bean.EquipReqularCheckItemBean;
import cc.comac.mom.datahandlers.entity.EquipReqularCheckDataHandler;
import cc.comac.mom.datahandlers.entity.EquipReqularCheckItemDataHandler;
import cc.comac.mom.entity.EquipReqularCheckEntity;
import cc.comac.mom.entity.EquipReqularCheckItemEntity;
import cc.comac.mom.util.IModificationFlags;
import cc.comac.mom.util.ResultBean;
@RestController
@Service
public class EquipReqularCheckItemServiceImpl implements EquipReqularCheckItemService {
	@Autowired
	private EquipReqularCheckDataHandler equipReqularCheckDataHandler;

	@Autowired
	private EquipReqularCheckItemDataHandler equipReqularCheckItemDataHandler;

	@Override
	public ResultBean saveBatch(@RequestBody EquipReqularCheckBean obj) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipReqularCheckItem> list = obj.getListItem();
		if (list == null || list.size() < 1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		for (EquipReqularCheckItem item : list) {
			if (item.getInnerData() == null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("数据为空");
				return resultBean;
			}
		}
		EquipReqularCheck equipReqularCheck = equipReqularCheckDataHandler
				.getById(list.get(0).getInnerData().getRegularCheckId(), 0, 0);
		if (equipReqularCheck == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("主类信息不存在");
			return resultBean;
		}
		// 数据存在新增数据和修改数据
		ArrayList<EquipReqularCheckItem> ListNew = new ArrayList<EquipReqularCheckItem>();
		ArrayList<EquipReqularCheckItem> ListUpdate = new ArrayList<EquipReqularCheckItem>();
		for (EquipReqularCheckItem item : list) {
			if (item.getModificationFlag() == IModificationFlags.NEW) {
				ListNew.add(item);
			} else if (item.getModificationFlag() == IModificationFlags.MODIFIED) {
				ListUpdate.add(item);
			}
		}
		if (ListNew != null && ListNew.size() > 0) {
			ResultBean resultBean2 = equipReqularCheckItemDataHandler.saveBatch(ListNew);
			if (!resultBean2.success) {
				return resultBean2;
			}
		}
		if (ListUpdate != null && ListUpdate.size() > 0) {
			ResultBean resultBean2 = equipReqularCheckItemDataHandler.updateBatch(ListUpdate);
			if (!resultBean2.success) {
				throw new RuntimeException(resultBean2.getMessage());
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		BigDecimal benchmarkTimeAll = new BigDecimal(0);
		map.put("row_status", 1);
		map.put("regular_check_id", list.get(0).getInnerData().getRegularCheckId());
		Collection<EquipReqularCheckItemEntity> listByMap = equipReqularCheckItemDataHandler.listByMap(map);
		if (listByMap != null && listByMap.size() > 0) {
			for (EquipReqularCheckItemEntity item : listByMap) {
				benchmarkTimeAll = benchmarkTimeAll.add(item.getBenchmarkTime());
			}
		}
		equipReqularCheck.getInnerData().setStandardWorkTime(benchmarkTimeAll);
		ResultBean resultBean2 = equipReqularCheckDataHandler.update(equipReqularCheck);
		if (!resultBean2.success) {
			throw new RuntimeException(resultBean2.getMessage());
		}
		return resultBean;
	}

	@Override
	public ResultBean deleteBatch(@RequestBody EquipReqularCheckItemBean obj) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipReqularCheckItem> list = obj.getList();

		ResultBean deleteResultBean = equipReqularCheckItemDataHandler.deleteBatch(list);
		if (!deleteResultBean.success) {
			return deleteResultBean;
		}
		EquipReqularCheckEntity reqularCheckEntity = equipReqularCheckDataHandler
				.getById(list.get(0).getInnerData().getRegularCheckId());
		if (reqularCheckEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("主类信息不存在");
			return resultBean;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		BigDecimal benchmarkTimeAll = new BigDecimal(0);
		map.put("row_status", 1);
		map.put("regular_check_id", list.get(0).getInnerData().getRegularCheckId());
		Collection<EquipReqularCheckItemEntity> listByMap = equipReqularCheckItemDataHandler.listByMap(map);
		if (listByMap != null && listByMap.size() > 0) {
			for (EquipReqularCheckItemEntity item : listByMap) {
				benchmarkTimeAll = benchmarkTimeAll.add(item.getBenchmarkTime());
			}
		}
		reqularCheckEntity.setStandardWorkTime(benchmarkTimeAll);
		EquipReqularCheck equipReqularCheck = new EquipReqularCheck();
		equipReqularCheck.setInnerData(reqularCheckEntity);
		ResultBean resultBean2 = equipReqularCheckDataHandler.update(equipReqularCheck);
		if (!resultBean2.success) {
			throw new RuntimeException(resultBean2.getMessage());
		}
		return resultBean;

	}

	@Override
	public ResultBean moveUp(@RequestBody EquipReqularCheckItem obj) {
		// TODO Auto-generated method stub
		return equipReqularCheckItemDataHandler.moveUp(obj);
	}

	@Override
	public ResultBean moveDown(@RequestBody EquipReqularCheckItem obj) {
		return equipReqularCheckItemDataHandler.moveDown(obj);
	}

	@Override
	public EquipReqularCheckItem getById(@RequestBody EquipReqularCheckItem obj) {
		return equipReqularCheckItemDataHandler.getById(obj.getInnerData().getId());
	}

	@Override
	public EquipReqularCheckItemBean selectByCondition(@RequestBody EquipReqularCheckItem obj) {
		return equipReqularCheckItemDataHandler.selectByCondition(obj);
	}

}
